package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.EventFieldConditionDetail;
import model.EventFieldsCondition;
import model.Field;
import utils.JdbcUtil;

public class EventFieldsConditionDao {

	public int save(EventFieldsCondition efc){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
	
		int result = 0;
		try {
			con = JdbcUtil.getCon();
			String sql = "insert into t_eventFieldsCondition(fid,ueid,condition,value) values(?,?,?,?)";
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, efc.getfId());
			pstmt.setInt(2, efc.getUeId());
			pstmt.setString(3, efc.getCondition());
			pstmt.setString(4, efc.getValue());
			
			//pstmt.setString(5, efc.getLocationValue());
			result = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}	
		return result;
	}
	
	/**
	 * 获得所有相同ueId的EventFieldsCondition
	 * @param ueId
	 * @return
	 */
	public List<EventFieldsCondition> getAllByUeId(int ueId){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
        
        List<EventFieldsCondition> efcList = new ArrayList<EventFieldsCondition>();
	
        String sql = "select id,fId,ueId,condition,value from t_eventFieldsCondition where ueid = ?";
        
		try {
			con = JdbcUtil.getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, ueId);
			rs = pstmt.executeQuery();
			while(rs.next()){
				EventFieldsCondition efc = new EventFieldsCondition();
				efc.setfId(rs.getInt("id"));
				efc.setfId(rs.getInt("fId"));
				efc.setUeId(rs.getInt("ueId"));
				efc.setCondition(rs.getString("condition"));
				efc.setValue(rs.getString("value"));
				//efc.setLocationValue(rs.getString("locationValue"));
				efcList.add(efc);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}	
		return efcList;
	}
	
	public List<EventFieldConditionDetail> getEventFieldConditionDetailsByUeId(int ueId){
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
        
        List<EventFieldConditionDetail> efcdList = new ArrayList<EventFieldConditionDetail>();
	
        String sql = "select efc.id as efcid,fId,f.fieldName,f.tId,f.type,ueId,condition,value from t_eventFieldsCondition efc,t_fields f where efc.fid = f.id and ueid = ?";
        
		try {
			con = JdbcUtil.getCon();
			pstmt = con.prepareStatement(sql);
			pstmt.setInt(1, ueId);
			rs = pstmt.executeQuery();
			while(rs.next()){
				EventFieldConditionDetail efcd = new EventFieldConditionDetail();
				EventFieldsCondition efc = new EventFieldsCondition();
				Field field =  new Field();
				
				efc.setfId(rs.getInt("efcid"));
				efc.setUeId(rs.getInt("ueId"));
				efc.setCondition(rs.getString("condition"));
				efc.setValue(rs.getString("value"));
				field.setId(rs.getInt("fid"));
				field.settId(rs.getInt("tId"));
				field.setFieldName(rs.getString("fieldName"));
				field.setType(rs.getString("type"));
				efcd.setEfc(efc);
				efcd.setField(field);
				//efc.setLocationValue(rs.getString("locationValue"));
				efcdList.add(efcd);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}	
		return efcdList;
	}
	
	public int getEventFieldCondition(String ueid)  {
		Connection con =null;  
        PreparedStatement pstmt = null;  
        ResultSet rs = null;
        
        int result = 0;
		String sql = "select * from t_eventFieldsCondition where ueid like '" + ueid + "'";
		try {
			con = JdbcUtil.getCon();
			pstmt = con.prepareStatement(sql);
			rs = pstmt.executeQuery();
			if(rs.next()){//有该用户注册事件
				result =  1;
			}
		}catch (SQLException e) {
			e.printStackTrace();
		}finally{
			JdbcUtil.closeAll(rs, pstmt,con);
		}
		return result;//没有该用户注册事件
	}
	
}
